package org.jivesoftware.smackx;

import java.util.Hashtable;
import java.util.Map;
import org.jivesoftware.smack.Connection;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.IQ.Type;
import org.jivesoftware.smack.provider.IQProvider;
import org.jivesoftware.smackx.packet.DefaultPrivateData;
import org.jivesoftware.smackx.packet.PrivateData;
import org.jivesoftware.smackx.provider.PrivateDataProvider;
import org.xmlpull.v1.XmlPullParser;

public class PrivateDataManager
{
  private static Map privateDataProviders = new Hashtable();
  private Connection connection;
  private String user;

  public PrivateDataManager(Connection paramConnection)
  {
    if (!(paramConnection.isAuthenticated()))
      throw new IllegalStateException("Must be logged in to XMPP server.");
    this.connection = paramConnection;
  }

  public PrivateDataManager(Connection paramConnection, String paramString)
  {
    if (!(paramConnection.isAuthenticated()))
      throw new IllegalStateException("Must be logged in to XMPP server.");
    this.connection = paramConnection;
    this.user = paramString;
  }

  public static void addPrivateDataProvider(String paramString1, String paramString2, PrivateDataProvider paramPrivateDataProvider)
  {
    String str = getProviderKey(paramString1, paramString2);
    privateDataProviders.put(str, paramPrivateDataProvider);
  }

  public static PrivateDataProvider getPrivateDataProvider(String paramString1, String paramString2)
  {
    String str = getProviderKey(paramString1, paramString2);
    return ((PrivateDataProvider)privateDataProviders.get(str));
  }

  private static String getProviderKey(String paramString1, String paramString2)
  {
    StringBuilder localStringBuilder = new StringBuilder();
    localStringBuilder.append("<").append(paramString1).append("/><").append(paramString2).append("/>");
    return localStringBuilder.toString();
  }

  public static void removePrivateDataProvider(String paramString1, String paramString2)
  {
    String str = getProviderKey(paramString1, paramString2);
    privateDataProviders.remove(str);
  }

  public PrivateData getPrivateData(String paramString1, String paramString2)
    throws XMPPException
  {
    1 local1 = new IQ(this, paramString1, paramString2)
    {
      public String getChildElementXML()
      {
        StringBuilder localStringBuilder = new StringBuilder();
        localStringBuilder.append("<query xmlns=\"jabber:iq:private\">");
        localStringBuilder.append("<").append(this.val$elementName).append(" xmlns=\"").append(this.val$namespace).append("\"/>");
        localStringBuilder.append("</query>");
        return localStringBuilder.toString();
      }
    };
    local1.setType(IQ.Type.GET);
    if (this.user != null)
      local1.setTo(this.user);
    String str = local1.getPacketID();
    PacketCollector localPacketCollector = this.connection.createPacketCollector(new PacketIDFilter(str));
    this.connection.sendPacket(local1);
    IQ localIQ = (IQ)localPacketCollector.nextResult(SmackConfiguration.getPacketReplyTimeout());
    localPacketCollector.cancel();
    if (localIQ == null)
      throw new XMPPException("No response from the server.");
    if (localIQ.getType() == IQ.Type.ERROR)
      throw new XMPPException(localIQ.getError());
    return ((PrivateDataResult)localIQ).getPrivateData();
  }

  public void setPrivateData(PrivateData paramPrivateData)
    throws XMPPException
  {
    2 local2 = new IQ(this, paramPrivateData)
    {
      public String getChildElementXML()
      {
        StringBuilder localStringBuilder = new StringBuilder();
        localStringBuilder.append("<query xmlns=\"jabber:iq:private\">");
        localStringBuilder.append(this.val$privateData.toXML());
        localStringBuilder.append("</query>");
        return localStringBuilder.toString();
      }
    };
    local2.setType(IQ.Type.SET);
    if (this.user != null)
      local2.setTo(this.user);
    String str = local2.getPacketID();
    PacketCollector localPacketCollector = this.connection.createPacketCollector(new PacketIDFilter(str));
    this.connection.sendPacket(local2);
    IQ localIQ = (IQ)localPacketCollector.nextResult(5000L);
    localPacketCollector.cancel();
    if (localIQ == null)
      throw new XMPPException("No response from the server.");
    if (localIQ.getType() == IQ.Type.ERROR)
      throw new XMPPException(localIQ.getError());
  }

  public static class PrivateDataIQProvider
  implements IQProvider
  {
    public IQ parseIQ(XmlPullParser paramXmlPullParser)
      throws Exception
    {
      Object localObject = null;
      int i = 0;
      while (true)
      {
        int j;
        do
        {
          String str1;
          String str2;
          while (true)
          {
            if (i != 0)
              return new PrivateDataManager.PrivateDataResult((PrivateData)localObject);
            j = paramXmlPullParser.next();
            if (j != 2)
              break label195;
            str1 = paramXmlPullParser.getName();
            str2 = paramXmlPullParser.getNamespace();
            PrivateDataProvider localPrivateDataProvider = PrivateDataManager.getPrivateDataProvider(str1, str2);
            if (localPrivateDataProvider == null)
              break;
            localObject = localPrivateDataProvider.parsePrivateData(paramXmlPullParser);
          }
          DefaultPrivateData localDefaultPrivateData = new DefaultPrivateData(str1, str2);
          int k = 0;
          while (true)
          {
            int l;
            label169: 
            do
              while (true)
              {
                String str3;
                do
                  while (true)
                  {
                    while (k != 0)
                      localObject = localDefaultPrivateData;
                    l = paramXmlPullParser.next();
                    if (l != 2)
                      break label169;
                    str3 = paramXmlPullParser.getName();
                    if (!(paramXmlPullParser.isEmptyElementTag()))
                      break;
                    localDefaultPrivateData.setValue(str3, "");
                  }
                while (paramXmlPullParser.next() != 4);
                localDefaultPrivateData.setValue(str3, paramXmlPullParser.getText());
              }
            while ((l != 3) || (!(paramXmlPullParser.getName().equals(str1))));
            label195: k = 1;
          }
        }
        while ((j != 3) || (!(paramXmlPullParser.getName().equals("query"))));
        i = 1;
      }
    }
  }

  private static class PrivateDataResult extends IQ
  {
    private PrivateData privateData;

    PrivateDataResult(PrivateData paramPrivateData)
    {
      this.privateData = paramPrivateData;
    }

    public String getChildElementXML()
    {
      StringBuilder localStringBuilder = new StringBuilder();
      localStringBuilder.append("<query xmlns=\"jabber:iq:private\">");
      if (this.privateData != null)
        this.privateData.toXML();
      localStringBuilder.append("</query>");
      return localStringBuilder.toString();
    }

    public PrivateData getPrivateData()
    {
      return this.privateData;
    }
  }
}